package com.lun.easy;

public class ReshapeTheMatrix {
    public int[][] matrixReshape(int[][] mat, int r, int c) {
        if(mat.length * mat[0].length != r * c)
        	return mat;
    	
        int[][] result = new int[r][c]; 
    	int rp = 0, cp = 0;
        
        for(int[] array : mat) 
        	for(int i : array) {
        		result[rp][cp++] = i;
        		if(cp == c) {
        			rp++;
        			cp = 0;
        		}
        	}
        
    	return result;
    }
}
